1.1) Create a class to represent a 2D point in space. The initializer should accept 2 inputs -- an x and a y coordinate -- and store those values as attributes of the object.
In [ ]:
1.2) Add a method of the class called distance_to() that accepts another point object, and returns the Euclidean distance between the two points. Add a check to this method to make sure point2 is a Point2D object.
In [ ]:
In [ ]:
1.3) Generalize the class so it can handle an arbitrary number of dimensions. hint: you can use *args to accept a variable number of arguments
In [ ]:
In [ ]:
In [ ]:
2) Write a Person class and an AddressBook class. You should be able to add and remove people from the address book by passing in Person objects. If you search for a phone number, or persons name, it should return their address.
In [ ]:
3) Write a class to represent a vending machine. Keep track of the prices of each item, and how much money has been put in to the machine. The following code should work when you're done:
vm = VendingMachine()
vm.restock("water", quantity=5, price=1.50)
vm.restock("iced tea", quantity=10, price=2.50)
vm.restock("soda", quantity=12, price=2.00)
vm.vend("water", 1.50)
print(vm.stock("water")) # how many waters are left
vm.vend("water", 1.25) # should fail, and return the money to the user
vm.vend("water", 1.75) # should work, and return 0.25 to the user
If the machine is out of a product, it should display a friendly message and fail to vend.
In [59]:
class Drink(object):
def __init__(self, name, quantity=0, price=1.50):
self.name = name
self.quantity = quantity
self.price = price
class VendingMachine(object):
def __init__(self):
self.ledgerbook = {}
print("You just bought a vending machine, you should restock it!")
def restock(self, name='water', quantity=5, price=1.50):
if name.lower() in self.ledgerbook.keys():
self.ledgerbook[name.lower()].quantity = self.ledgerbook[name.lower()].quantity + quantity
else :
tasty_bev = Drink(name, quantity=quantity, price=price)
self.ledgerbook[tasty_bev.name.lower()] = tasty_bev
def empty_item(self,name):
if name.lower() in self.ledgerbook.keys():
del self.ledgerbook[name.lower()]
else:
print("You don't have any {} in your vending machine".format(name))
def vend(self, name, money):
if name.lower() in self.ledgerbook.keys() and self.ledgerbook[name.lower()].quantity > 0:
change = money-self.ledgerbook[name.lower()].price
if money >= self.ledgerbook[name.lower()].price:
self.ledgerbook[name.lower()].quantity -= 1
print("Plop-thunk! \n Here is your tasty {}!".format(name))
if change > 0:
print("and here is ${} in change".format(change))
else :
print("Please insert ${} more.".format(abs(change)))
else:
print("We don't have any {}, please make an alternative selection!".format(name))
def stock(self, name):
if name.lower() in self.ledgerbook.keys() and self.ledgerbook[name.lower()].quantity > 0:
print("We have {} of {} in stock".format(self.ledgerbook[name.lower()].quantity, name))
return self.ledgerbook[name.lower()].quantity
else :
print("We are out of {}".format(name))
return 0
In [54]:
lunchroom = VendingMachine()
In [55]:
lunchroom.restock('Pepsi',5,1.00)
In [50]:
print lunchroom.ledgerbook['water'].price
print lunchroom.ledgerbook['water'].quantity
In [30]:
lunchroom.ledgerbook[/]
Out[30]:
In [60]:
vm = VendingMachine()
vm.restock("water", quantity=5, price=1.50)
vm.restock("iced tea", quantity=10, price=2.50)
vm.restock("soda", quantity=12, price=2.00)
vm.vend("water", 1.50)
print(vm.stock("water")) # how many waters are left
vm.vend("water", 1.25) # should fail, and return the money to the user
vm.vend("water", 1.75) # should work, and return 0.25 to the user
In [63]:
vm.stock('Iced Tea')
Out[63]:
plot(arange(0,10))
In [ ]:
import